using PyCall
include("vec.jl")

@pyimport pygame

WIDTH = 512
HEIGHT = 512

top = Vec.Vec2(240, 20)
root = Vec.Vec2(280, 470)
unit_dir = root - top
unit_dir = Vec.Normalize(unit_dir)
pen_color = (0, 255, 0)
max_dist = Vec.Length(top - root)

min_init_node_count = 100       # 初始随机点数量下限
max_init_node_count = 200       # 初始随机点数量下限

function Render(surface)
    surface.fill((0, 0, 0))
    surface.blit(leaf, (0, 0))

    for node in nodes
        x = Vec.X(node)
        y = Vec.Y(node)
        surface.set_at((x, y), pen_color)
    end
end

function GetInitNodeCount()
    rand(min_init_node_count:max_init_node_count)
end

# 在叶子上随机分布点
function GenNodes(nodeCount)
    nodes = []
    while length(nodes) < nodeCount
        x::Int32 = rand(0 : WIDTH - 1)
        y::Int32 = rand(0 : HEIGHT - 1)
    
        clr =  render_surface.get_at((x, y)) 
        
        if clr == (255, 255, 255, 255)
            push!(nodes, Vec.Vec2(x, y))
        end
    end

    return nodes
end

pygame.init()

render_surface = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("leaf")
render_surface.fill((0, 0, 0))
leaf = pygame.image.load("./images/1182.jpg")
render_surface.blit(leaf, (0, 0))

init_node_count = GetInitNodeCount()
nodes = GenNodes(init_node_count)

while true
    Render(render_surface)
    for event in pygame.event.get()
        if event.type == pygame.QUIT
            pygame.quit()
            return
        end
    end

    pygame.display.update()
end
